package com.renaud.tvSpy;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import com.renaud.tvSpy.graphicsTool.Capter;
import com.renaud.tvSpy.graphicsTool.ImageLoadException;
import com.renaud.tvSpy.graphicsTool.JImageBuffer;
import com.renaud.tvSpy.graphicsTool.SimpleImageLoader;



public class RepaintTask extends Thread implements NativeKeyListener {
	
	private View view;
	private Capter capter;
	private JImageBuffer backbuffer;
	private float scale;
	private boolean loupeOn;
	private Loupe loupe;
	private boolean reading;
	private Point position;


	private static Image curseur;
	static{
		try {
			curseur = new SimpleImageLoader().getImage(System.getProperty("user.dir")+"/src/main/resources/images/cursor-icon.png");
		} catch (ImageLoadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	



	public RepaintTask(View fenetre, Capter capter, JImageBuffer backbuffer, float scale) {
		this.view = fenetre;
		this.capter = capter;
		this.backbuffer = backbuffer;
		this.scale = scale;
		this.loupe = new Loupe(backbuffer,scale,150,100);
		this.position = new Point();
		this.loupe.setPosition(this.position);
	}


	public RepaintTask(View fenetre, Capter capter, JImageBuffer backbuffer, float scale,Loupe loupe) {
		this.view = fenetre;
		this.capter = capter;
		this.backbuffer = backbuffer;
		this.scale = scale;
		this.loupe = loupe;
		this.position = new Point();
		this.loupe.setPosition(this.position);
	}




	@Override
	public void run() {
		if(capter != null && this.reading){
			Image screenShot = this.capter.getImage();

			
			this.backbuffer.drawImage(screenShot, 0, 0, 0, 0, 0, this.scale, 1.0f);
			
			
			Point mp = MouseInfo.getPointerInfo().getLocation();
			String activeId = null;
			Rectangle activeRect = null;
			for (GraphicsDevice device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
		        for (GraphicsConfiguration config : device.getConfigurations()) {
		            final Rectangle gcBounds = config.getBounds();
		            if (gcBounds.contains(mp)) {
		               activeRect = gcBounds;
		               activeId = device.getIDstring();
		            }
		        }
		    }
			

			
			if(activeId.equals(view.getActiveDevice().getDeviceId())){
				
				position.x = (int) ((mp.x-activeRect.x) * this.scale);
				position.y = (int) ((mp.y-activeRect.y) * this.scale);
				
				if(this.loupeOn){
					this.loupe.setScreenshot(screenShot);
					this.loupe.draw();
				}
				
				this.backbuffer.drawImage(curseur, position.x, position.y, 0, 0, 0, this.scale, 1.0f);
			}
			this.view.repaint();
		}
	
	}




	public JImageBuffer getBackbuffer() {
		return backbuffer;
	}

	public void setBackbuffer(JImageBuffer backbuffer) {
		this.backbuffer = backbuffer;
		this.loupe.setBackBuffer(backbuffer);
	}

	public Capter getCapter() {
		return capter;
	}

	public void setCapter(Capter capter) {
		this.capter = capter;
	}

	public Loupe getLoupe() {
		return loupe;
	}


	public void setLoupe(Loupe loupe) {
		this.loupe = loupe;
		this.loupe.setPosition(this.position);
	}

	public void startReading(){
		this.reading = true;
	}
	
	public void stopReading(){
		this.reading = false;
	}

/********************************/
	@Override
	public void nativeKeyPressed(NativeKeyEvent e) {
		if(e.getKeyCode() == NativeKeyEvent.VK_CONTROL){
			this.loupeOn = true;
		}
	}


	@Override
	public void nativeKeyReleased(NativeKeyEvent e) {
		if(e.getKeyCode() == NativeKeyEvent.VK_CONTROL){
			this.loupeOn = false;
		}
	}


	@Override
	public void nativeKeyTyped(NativeKeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}


}
